package com.skyz.wrap.utils;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;

import com.hjq.toast.ToastUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class SaveBitmap {
    private SaveBitmap(){

    }
    private static SaveBitmap instance = new SaveBitmap();

    public static SaveBitmap getInstance(){
        return  instance;
    }

    public Uri saveBitmap2Gallery(Context context, String fileName, Bitmap bitmap) throws IOException {
        OutputStream fos=null;
        Uri insertUri = null;
        File image=null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            ContentValues contentValues = new ContentValues();
            contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
            contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
            contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
            insertUri= context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
            fos = context.getContentResolver().openOutputStream(insertUri);
        }else {
            File imagesDir =
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

            if (!imagesDir.exists()) {
                imagesDir.mkdir();
            }
            image = new File(imagesDir, fileName);
            fos = new FileOutputStream(image);
        }
        if (fos!=null){
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        }
        if (image!=null){
            String insertImage = MediaStore.Images.Media.insertImage(
                    context.getContentResolver(), image.getAbsolutePath(), fileName, null);
            File file = new File(getRealPathFromURI(Uri.parse(insertImage),context));
            updatePhotoMedia(Uri.fromFile(file),context);
            return Uri.parse(insertImage);
        }else {

            File file = new File(getRealPathFromURI(insertUri,context));
            updatePhotoMedia(Uri.fromFile(file),context);
            return insertUri;
        }

    }

    public void convertViewToBitmap(View view) throws IOException {
        view.setDrawingCacheEnabled(true);
//        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        saveBitmap2Gallery(view.getContext(),new TimeUtil().getCurrentDate("yyy-MM-dd-HHmmss")+"haibao.jpg",bitmap);
        view.setDrawingCacheEnabled(false);
    }

    public Bitmap getBtMap(View view){
        view.setDrawingCacheEnabled(true);
//        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        view.setDrawingCacheEnabled(false);
        return bitmap;
    }
    public Uri getBtMapUri(View view) throws IOException {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        Uri uri =saveBitmap2Gallery(view.getContext(),new TimeUtil().getCurrentDate("yyy-MM-dd-HHmmss")+"haibao.jpg",bitmap);
        view.setDrawingCacheEnabled(false);
        return uri;
    }

    //更新图库
    private  void updatePhotoMedia(Uri uri ,Context context){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(uri);
        context.sendBroadcast(intent);
    }
    //得到绝对地址
    private static String getRealPathFromURI(Uri contentUri,Context context) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String fileStr = cursor.getString(column_index);
        cursor.close();
        return fileStr;
    }
}
